<?php

namespace app\merchants\controller;

use app\common\validate\shop\ShopSalesmanValid;
use app\merchants\loader\Services;
use think\Exception;

/**
 * 导购管理
 */
class ShopSalesman extends ApiBase
{
    /**
     * 导购分页
     */
    public function salesman_page()
    {
        try {
            $data = input('get.');

            $where[] = ['mer_id', '=', $this->adminUser['mer_id']];
            if (!empty($data['keyword'])) {
                $where[] = ['open_id|name|mobile', 'like','%'.trim($data['keyword']).'%'];
            }
            if (!empty($this->adminUser['sub_admin_ids'])) {
                $admin_id = $this->adminUser['sub_admin_ids'];
                $where[] = ['shop_id', 'in', function ($query) use ($admin_id) {
                    $query->name('shop')->whereIn('op_user', $admin_id)->field('id');
                }];
            }
            if (!empty($data['shop_id'])) {
                $where[] = ['shop_id', '=', $data['shop_id']];
            }

            if (!empty($data['stime']) && empty($data['etime'])) {
                $where[] = ['create_at', '>=', $data['stime']];
            }
            if (empty($data['stime']) && !empty($data['etime'])) {
                $where[] = ['create_at', '<=', $data['etime']];
            }
            if (!empty($data['stime']) && !empty($data['etime'])) {
                $where[] = ['create_at', 'between time', [$data['stime'], $data['etime']]];
            }
            if (isset($data['recheck']) && $data['recheck'] !== 'all') {
                $where[] = ['recheck', '=', $data['recheck']];
            }

            $page = Services::ShopSalesman()->salesmanPage(
                $where,
                $data['page'] ?: 1,
                $data['page_size'] ?: 30);

            api_result(API_SUCCESS, '请求成功', $page);
        } catch (Exception $e) {
            api_result($e);
        }

    }

    /**
     *  导购列表
     */
    public function salesman_list()
    {
        try {
            $data = input('get.');
            $where[] = ['mer_id', '=', $this->adminUser['mer_id']];
            if (!empty($data['keyword'])) {
                $where[] = ['name', '=', $data['keyword']];
            }
            // 如果店铺不为空,按店铺查找
            $shopId = (int)($data['shop_id'] ?? 0);
            if ($shopId > 0) {
                $where[] = ['shop_id', '=', $shopId];
            }
            $page = Services::ShopSalesman()->salesmanList(
                $where,
                $data['list'] ?: 10);

            api_result(API_SUCCESS, '请求成功', $page);
        } catch (Exception $e) {
            api_result($e);
        }

    }


    /**
     *  导购详情
     */
    public function salesman_info()
    {
        try {
            $data = input('get.');
            validate(ShopSalesmanValid::class)->scene('salesman_info')->check($data);
            $info = Services::ShopSalesman()->salesmanInfo($data['id']);
            api_result(API_SUCCESS, '请求成功', $info);
        } catch (Exception $e) {
            api_result($e);
        }

    }


    /**
     *  导购积分调整
     */
    public function salesman_save_point()
    {
        try {

            $data = input('post.');
            validate(ShopSalesmanValid::class)->scene('salesman_save_point')->check($data);
            Services::ShopSalesman()->salesmanSavePoint(
                $data['id'],
                $data['point'],
                $data['memo'],
                $data['type']);

            api_result(API_SUCCESS, '请求成功', $data['id']);


        } catch (Exception $e) {
            api_result($e);
        }

    }


    /**
     * 导购修改
     */
    public function salesman_save()
    {
        try {
            $data = input('post.');
            validate(ShopSalesmanValid::class)->scene('salesman_save')->check($data);
            Services::ShopSalesman()->salesmanSave(
                $data['shop_id'],
                $this->adminUser['mer_id'],
                $data['name'],
                $data['mobile'],
                $data['id']);

            api_result(API_SUCCESS, '请求成功');
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 导购修改状态
     */
    public function salesman_save_status()
    {
        try {

            $data = input('post.');
            validate(ShopSalesmanValid::class)->scene('salesman_save_bind')->check($data);
            Services::ShopSalesman()->saveBind($data['id'], $data['status']);
            api_result(API_SUCCESS, '请求成功', $data['id']);
        } catch (Exception $e) {

        }
    }


    /**
     * 导购清除绑定
     */
    public function salesman_eliminate_wx_info()
    {
        try {

            $data = input('post.');
            validate(ShopSalesmanValid::class)->scene('salesman_eliminate_wx_info')->check($data);
            //清空绑定所有微信信息
            $updata_data = [
                'open_id' => '',
                'union_id' => '',
                'nickname' => '',
                'address' => '',
                'lat' => '',
                'lng' => '',
                'head_img' => '',
                'bind_wechat' => 0,
                'recheck' => 0,
                'status' => 0,
            ];
            Services::ShopSalesman()->eliminateWxInfo($data['id'], $this->adminUser['mer_id'], $updata_data);
            api_result(API_SUCCESS, '请求成功', $data['id']);
        } catch (Exception $e) {

        }
    }


    /**
     * 废弃-清除地址绑定
     */
    private function salesman_eliminate_wx_address()
    {
        try {

            $data = input('post.');
            validate(ShopSalesmanValid::class)->scene('salesman_eliminate_wx_address')->check($data);
            //清空绑定地址信息
            $updata_data = [
                'address' => '',
                'lat' => '',
                'lng' => '',
            ];
            Services::ShopSalesman()->eliminateWxInfo($data['id'], $this->adminUser['mer_id'], $updata_data);
            api_result(API_SUCCESS, '请求成功', $data['id']);
        } catch (Exception $e) {

        }
    }

    /**
     * 导购修改审核状态
     */
    public function recheck()
    {
        try {
            $params = $this->request->post();
            if (empty($params['id'])) {
                api_exception(400, 'ID不能为空');
            }
            Services::ShopSalesman()->editShopper($params, $this->merId);
            api_result(API_SUCCESS, '审核成功');
        } catch (Exception $e) {
            api_result($e);
        }

    }
}
